﻿package com.tdzx.util;


import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.tdzx.entity.Member;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharePrefenceUtil {
	public static final String SHAREPREFENCE_NAME = "hdzx";
	public static final String DEFAULT_CITY = "city";
	public static final String DEFAULT_CITY_ID = "city_id";
	public static final String LOCATION_LATITUDE = "latitude";
	public static final String LOCATION_LONGTITUDE = "longtitude";
	public static final String MESSAGE_PUSH = "message_push";
	public static final String ROTATESTATUS = "rotate";  //大于0表示地图模式，小于0表示列表模式
	public static final String XINLANG_LOCK = "xinlang_lock";
	public static final String TENGXUN_LOCK = "tengxun_lock";
	public static final String WEIXIN_LOCK = "weixin_lock";
	
	public static final String ALLCITY = "all";
	public static final String CITYCNTER = "cityCenter";
	public static final String CITYIDFORWEATHER = "cityforweather";
	
	public static final String PRIORITY = "priority"; 
	public static final int LIST = -1; // >0的数
	public static final int MAP = 1;  // <0的数
	public static final int NOTCHOSE = -1; //第一次进入没有选择
	
	
	public static boolean getXinlangLock(Context context) {
		boolean xinlang_flag = false;
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		xinlang_flag = spf.getBoolean(XINLANG_LOCK, false);
		return xinlang_flag;
	}
	public static void setXinlangLock(Context context,boolean xinlang_flag) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putBoolean(XINLANG_LOCK,xinlang_flag);
		editor.commit();
	}
	
	public static boolean getTengxunLock(Context context) {
		boolean tengxun_flag = false;
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		tengxun_flag = spf.getBoolean(TENGXUN_LOCK, false);
		return tengxun_flag;
	}
	public static void setTengxunLock(Context context,boolean tengxun_flag) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putBoolean(TENGXUN_LOCK,tengxun_flag);
		editor.commit();
	}

	public static boolean getWeixinLock(Context context) {
		boolean weixin_flag = false;
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		weixin_flag = spf.getBoolean(WEIXIN_LOCK, false);
		return weixin_flag;
	}
	public static void setWeixinLock(Context context,boolean weixin_flag) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putBoolean(WEIXIN_LOCK,weixin_flag);
		editor.commit();
	}

	public static boolean getMessagePush(Context context) {
		boolean message_push = true;
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		message_push = spf.getBoolean(MESSAGE_PUSH, true);
		return message_push;
	}
	
	public static void setMessagePush(Context context,boolean message_push){
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putBoolean(MESSAGE_PUSH, message_push);
		editor.commit();
	}
	
	public static String getCity(Context context) {
		String city = "";
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		city = spf.getString(DEFAULT_CITY, "");
		return city;
	}
	
	public static void setCity(Context context,String city) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putString(DEFAULT_CITY, city);
		editor.commit();
	}
	
	/*public static void setCityCenter(Context context,String center) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putString(CITYCNTER, center);
		editor.commit();
	}
	
	public static String getCityCenter(Context context) {
		String city_center = "";
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		city_center = spf.getString(CITYCNTER, "");
		return city_center;
	}*/
	
	public static void setCityID(Context context,String id) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putString(DEFAULT_CITY_ID, id);
		editor.commit();
	}
	
	public static String getCityID(Context context) {
		String city_id = "";
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		city_id = spf.getString(DEFAULT_CITY_ID, "");
		return city_id;
	}
	
	public static void setCityIDForWeather(Context context,String id) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putString(CITYIDFORWEATHER, id);
		editor.commit();
	}
	
	public static String getCityIDForWeather(Context context) {
		String city_id = "";
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		city_id = spf.getString(CITYIDFORWEATHER, "");
		return city_id;
	}
	
	public static void setCenter(Context context,String all) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putString(ALLCITY, all);
		editor.commit();
	}
	
	public static String getCenter(Context context) {
		String city_center = "";
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		city_center = spf.getString(ALLCITY, "");
		return city_center;
	}
	
	public static int getRotateStatus(Context context) {
		int status = NOTCHOSE;
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		status = spf.getInt(ROTATESTATUS, NOTCHOSE);
		return status;
	}
	
	
	public static void setRotateStatus(Context context,int value) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putInt(ROTATESTATUS, value);
		editor.commit();
	}

	public static String getUserID(Context context) {
		String id = "";
		SharedPreferences sp = context.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
		id = sp.getString("id", "");
		return id;
	}
	
	public static void setUserID(Context context,String id) {
		SharedPreferences sp = context.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString("id", id);
		editor.commit();
	}
	
	public static boolean checkLogin(Context context) {
		boolean login = false;
		String id = "";
		SharedPreferences sp = context.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
		id = sp.getString("id", "");
		if(!id.equals("")) {
			login = true;
		}
		return login;
	}
	
	public static GeoPoint getGeopoint(Context context) {
		GeoPoint point = null;
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		int latitude = spf.getInt(LOCATION_LATITUDE, 0);
		int longtitude = spf.getInt(LOCATION_LONGTITUDE, 0);
		if(latitude != 0 && longtitude != 0) {
			point = new GeoPoint(latitude, longtitude);
		}
		return point;
	}
	
	public static void setGeopoint(int latitude,int longtitude,Context context) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putInt(LOCATION_LATITUDE, latitude);
		editor.putInt(LOCATION_LONGTITUDE, longtitude);
		editor.commit();
	}
	
	public static void clear(Context context){
		SharedPreferences sp = context.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		editor.clear();
		editor.commit();
	}
	
	public static void setChosed(Context context,int priority) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putInt(PRIORITY, priority);
		editor.commit();
	}
	
	public static int getPriority(Context context) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		return spf.getInt(PRIORITY, NOTCHOSE);
	}
	
	public static void setLaucherImg(Context context,String url) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putString("fileUrl", url);
		editor.commit();
	}
	
	public static String getLaucherUrl(Context context) {
		String url = "";
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		url = spf.getString("fileUrl", "");
		return url;
	}
	
	
	public static void setUser(Context context,Member member) {
		SharedPreferences spf = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		if(member.getMember_Nick_Name() != null && !member.getMember_Nick_Name().equals("") && !member.getMember_Nick_Name().equals("null")) {
			editor.putString("nickName", member.getMember_Nick_Name());
		}
		if(member.getMemeber_Sex() != null && !member.getMemeber_Sex().equals("") && !member.getMemeber_Sex().equals("null")) {
			editor.putString("sex", member.getMemeber_Sex());
		}
		if(member.getMember_Address() != null && !member.getMember_Address().equals("") && !member.getMember_Address().equals("null")) {
			editor.putString("city", member.getMember_Address());
		}
		if(member.getMember_Avatar() != null && !member.getMember_Avatar().equals("") && !member.getMember_Avatar().equals("null")) {
			editor.putString("ava", member.getMember_Avatar());
		}
		if(member.getMember_Phone() != null && !member.getMember_Phone().equals("") && !member.getMember_Phone().equals("null")) {
			editor.putString("phone", member.getMember_Phone());
		}
		if(member.getMember_Info() != null && !member.getMember_Info().equals("") && !member.getMember_Info().equals("null")) {
			editor.putString("info", member.getMember_Info());
		}
		editor.commit();
	}
	
	public static void clearUser(Context context) {
		SharedPreferences spf = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.clear();
		editor.commit();
	}
	
	public static Member getMember(Context context) {
		Member member = new Member();
		SharedPreferences spf = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
		String nickName = spf.getString("nickName", "");
		String sex = spf.getString("sex", "");
		String city = spf.getString("city", "");
		String ava = spf.getString("ava", "");
		String phone = spf.getString("phone", "");
		String info = spf.getString("info", "");
		member.setMember_Address(city);
		member.setMember_Avatar(ava);
		member.setMember_Nick_Name(nickName);
		member.setMemeber_Sex(sex);
		member.setMember_Phone(phone);
		member.setMember_Info(info);
		return member;
	}
}
